package com.hz.cheetah.sample.client;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.hz.cheetah.sample.api.HelloService;
import com.hz.cheetah.sample.api.HelloService2;
import com.hz.cheetah.sample.api.HelloServiceAsync;
import com.hz.cheetah.sample.api.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.concurrent.CountDownLatch;


@ContextConfiguration(locations = {"classpath*:/spring.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class HelloClient2 {

    @Autowired()
    @Qualifier("HelloService")
    HelloService helloService;
    @Autowired()
    @Qualifier("HelloService2")
    HelloServiceAsync helloService2;

    @Test
    public void testPerson() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        String result = helloService2.hello(new Person("chuan", "yang"));
        System.out.println(result);
        ListenableFuture<String> ss =  helloService2.helloAsync(new Person("chuan", "yang"));
        Futures.addCallback(ss, new FutureCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("############################# " + result);
                latch.countDown();
            }

            @Override
            public void onFailure(Throwable t) {
                t.printStackTrace();
            }
        });
        latch.await();
    }
}
